<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Title</title>
  <link rel="stylesheet" href="/js/layui/css/layui.css" media="all">
  <link rel="stylesheet" href="/css/common.css" media="all">
  <link rel="stylesheet" href="../viewstyle/css/searchform.css">
  <script src="/js/layui/layui.all.js"></script>
  <script src="/admin/js/token.js"></script>
  <script src="../viewstyle/config/config2.js"></script>
</head>
<body>

<div class="layui-fluid">
  <div class="layui-card">
    <div class="layui-form layui-card-header layuiadmin-card-header-auto">
      <div style="text-align: center;">
        <!--搜索-->
        <div class="layui-inline">
          <input class="layui-input" name="id" id="apkname" placeholder="应用名称" autocomplete="off">
        </div>

        <div class="layui-inline">
          <button class="layui-btn layuiadmin-btn-admin" id="btnsearch" lay-submit lay-filter="LAY-user-back-search">
            <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>搜索
          </button>
        </div>
      </div>
    </div>

    <!--表格-->
    <div class="layui-card-body">
      <table class="layui-hide" id="t" lay-filter="t"></table>
    </div>
  </div>
</div>

</body>
<!--表格头部按钮-->
<script type="text/html" id="tb">
  <div class="layui-container">
    <a class="layui-btn layui-btn-sm layui-icon layui-icon-add-1" lay-event="addOrdinaryApk">新增应用</a>
    <a class="layui-btn layui-btn-sm layui-icon layui-icon-list" lay-event="ShowSystemApk">查看系统APK</a><!--lay-event="showSystemApk"-->
  </div>
</script>

<!--表格右侧按钮-->
<script type="text/html" id="caozuo">
  <button class="layui-btn layui-btn-primary layui-btn-xs" lay-event="public">
    {{#if(d.apk_is_it_public=='公开'){ }}
    关闭
    {{# }else{}}
    公开
    {{# }}}
  </button>
  <button class="layui-btn layui-btn-xs" lay-event="edit">编辑</button>
  <button class="layui-btn layui-btn-xs" lay-event="update">更新站点</button>
</script>

<script>
  //表格渲染
    var token = layui.data("local_store").token;
    var table = layui.table
    table.render({
      elem: '#t',
      id: 'tt',
      toolbar: '#tb',
      url: '/admin/apk/searchApkWithPageById?token='+token,
      cols: [
        [{
          field: 'apk_id',
          title: '编号',
          width: '6%',
          align: 'center'
        },
          {
            field: 'apk_name',
            title: '应用名称',
            width: '10%',
            align: 'center'
          },
          {
            field: 'apk_package',
            title: '应用包名',
            width: '12%',
            align: 'center'
          },
          {
            field: 'apk_url',
            title: '文件名',
            width: '11.5%',
            align: 'center',
            hide:true
          },
          {
            field: 'apk_version',
            title: '版本',
            width: '6%',
            align: 'center'
          },
          {
            field: 'admin_name',
            title: '上传用户',
            width: '10%',
            align: 'center'
          },

          {
            field: 'apk_shenhe',
            title: '审核状态',
            width: '10%',
            align: 'center'
          },

          {
            field: 'apk_picture_url',
            title: '应用图片',
            width: '10%',
            align: 'center',
            templet: function(d) {
                return "<img width='50px' height='50px' src='" + d.apk_picture_url + "'></img>";
              }
          },
          {
            field: 'apk_is_it_public',
            title: '是否公开',
            width: '10%',
            align: 'center',
          },
          {
            title: '操作',
            fixed: 'right',
            width: '20%',
            align: 'center',
            toolbar: '#caozuo'
          }
        ]
      ],
      page: true,
      limit:40
    });


    //搜索
    $("#btnsearch").click(function() {
      table.reload('tt', {
        where: {
          "apk_name": $('#apkname').val()
        },
        page: {
          curr: 1
        }
      })
    });

    //键盘回车监听
    $(window).keypress(function (event) {
      if(event.which === 13){
        $('#btnsearch').click();
      }
    })

    //添加和返回的监听事件
    table.on('toolbar(t)', function (obj) {
      var e = obj.event;
      switch (e) {
        case 'addOrdinaryApk':
          layer.open({
            title:"新增应用",
            content:"AddOrdinaryApk.html",
            type:2,
            area:["100%","100%"]
          })
          break;
        case 'ShowSystemApk':
          layer.open({
            title:"查看系统APK",
            content:"ShowSystemApk.html",
            type:2,
            area:["100%","100%"]
          });
          break;


      }
    })

    //右侧tool监听事件
    table.on('tool(t)', function(obj) {
      var data = obj.data; //获得当前行数据
      var e = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
      var tr = obj.tr; //获得当前行 tr 的 DOM 对象（如果有的话）
        switch(e) {
        case "public":
          var lay = layer.confirm("确定要" + $(tr).find("button:first").html() + "吗？", {
              title: "信息",
            btn: ["确定", "取消"]
                  },
            function (index) {
              $.ajax({
                url: "/admin/apk/updateApkByApkIdWithPublic",
                type: "post",
                data: {
                  data: data,
                },
                success: function (res) {
                  console.log(res)
                  if (res.code == 200) {
                      var public = tr.find("button:first").html();
                    if(res.data.apkIsItPublic=="不公开"){
                      tr.find("td:eq(8)").html("不公开");
                      tr.find("button:first").html("公开");
                    }else{
                      tr.find("td:eq(8)").html("公开");
                      tr.find("button:first").html("关闭");
                    }
                    layer.msg(public+'成功',{
                      icon:1
                    });
                    setTimeout(function () {
                      layer.close(lay);
                    }, 1000)
                    return;
                  }
                  layer.msg(public+'失败',{
                    icon:2,
                  });
                },
                error:function () {
                  layer.msg('网络原因编辑失败',{
                    icon:2
                  });
                }
              })
            }
          );

          break;

        case "edit":
          layui.data("a", {
            key: "apk",
            value: data
          }); //存值
            //得到当前点击行的用户id
          var adminTr = data.apk_admin
          $.ajax({
              type:'post',
              url:'/admin/apk/isItQuanxian',
              data:{
                  token:token,
                  adminTr:adminTr
              },
              success:function (res) {
                  if(res.code==500){
                      layer.msg(res.data, {
                          icon: 2
                      });
                      return;
                  }
                  layer.open(
                          {
                            title: '编辑普通应用',
                            content: 'EditOrdinaryApk.html',
                            type: 2,
                            area: ['100%', '100%'],
                            end:function () {
                              $(".layui-laypage-btn").click();
                            }
                          }
                  )

              },
              error: function(res) {
                  layer.msg('服务器异常或网络原因', {
                      icon: 2
                  });
              }
          });

          break;

        case "del":
          var lay = layer.confirm("确认删除吗?", {
                    title: "信息",
                    btn: ["确定", "取消"]
                  },
                  function(index) {
                    $.ajax({
                      type: "post",
                      url: '/admin/apk/deleteApkByApkId',
                      data: {
                        apkId: data.apk_id
                      },
                      success: function(res) {
                        if(res.code == 200) {
                          $(tr).remove();
                          layer.msg("删除成功", {
                            icon: 1
                          });
                          return;

                          setTimeout(function() {
                            layer.close(lay);
                          }, 1000);

                          return;
                        }
                        layer.msg("删除失败", {
                          icon: 2
                        });

                      },
                      error: function(res) {
                            layer.msg('服务器异常或网络原因', {
                                icon: 2
                            });
                        }
                    });
                  }
          );
          break;
          case 'update':
            var close = layer.confirm("确定更新到所有站点吗？", {
              title: "提示",
              btn: ["确定", "取消"],
              btn1: function () {
                $.ajax({
                  type: "get",
                  url: "/admin/apk/update", //-----------------------
                  data: {
                    apkId: data.apk_id,
                  },
                  success: function (res) {
                    if (res.code == 200) {

                      layer.msg("更新成功", {
                        icon: 1,
                        time: 1000
                      });
                      layer.close(close);
                      return;
                    }
                    layer.msg(res.data, {
                      icon: 2
                    })
                  },
                  error: function () {
                    layer.msg('网络原因更新失败', {
                      icon: 2
                    })
                  }
                })
              }
            })
            break;


      }
    });
</script>
</html>
